package
{
	import laya.display.Animation;
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.ui.Button;
	import laya.utils.Ease;
	import laya.utils.Handler;
	import laya.utils.Tween;
	
	/**
	 * 动画基础 教程练习 
	 * @author kungfu
	 * 
	 */	
	public class AnimationSample
	{
		private var _roleUrlAtlas:String = "res/atlas/role.atlas";
		private var _roleAni:Animation;
		private var _compBox:Sprite;
		public function AnimationSample()
		{
			createByAtlas();
			//useAni();
		}
		/**
		 *播放ani文件动画 
		 * 
		 */		
		private function useAni():void
		{
			var ani:Animation = new Animation();
			Laya.stage.addChild(ani);
			ani.loadAnimation("TimeLine.ani"); //.ani用到的资源需要预加载
			ani.pos(100,100);
			//ani.play(); //默认播放第一个动画
			ani.play(0,true,'pivot'); //播放指定名称的动画
		}
		/**
		 * 通过atlas文件播放动画 
		 * 
		 */		
		private function createByAtlas():void
		{
			//通过创建动画模板 可以播放指定某部分动画 创建模板资源必须预加载	
			//创建动画模板消耗内存 节省CPU
			Animation.createFrames(aniUrls(AnimationName.DIE,8),AnimationName.DIE);
			Animation.createFrames(aniUrls(AnimationName.ATTACK,7),AnimationName.ATTACK);
			Animation.createFrames(aniUrls(AnimationName.MOVE,8),AnimationName.MOVE);
			Animation.createFrames(aniUrls(AnimationName.MOVE,8),AnimationName.MOVE);
			Animation.createFrames(aniUrls(AnimationName.STAND,7),AnimationName.STAND);
			
			_roleAni = new Animation();
			_roleAni.autoSize = true;
			_roleAni.interval = 120; //设置播放速度
			//通过加载图集文件播放动画图集里的所有动画
			//_roleByAtlas.loadAtlas(_roleUrlAtlas,Handler.create(this,onRoleLoaded)); 
			Laya.stage.addChild(_roleAni);						
			_roleAni.stop();
			_roleAni.play(0,true,AnimationName.STAND); //播放动画模板
			_roleAni.pivotX = _roleAni.width/2;
			_roleAni.pivotY = _roleAni.height;
			_roleAni.pos(300,300,true);
			_compBox = new Sprite();
			Laya.stage.addChild(_compBox);
			//_compBox.x = 0;//_roleAni.x;
			//_compBox.y = _roleAni.y + _roleAni.height;
			
			for (var i:int = 0; i < AnimationName.names.length; i++) 
			{
				var btn:Button = new Button("comp/button.png",AnimationName.names[i]);
				btn.labelSize = 30;
				_compBox.addChild(btn);
				btn.width = 200;
				btn.height = 100;
				btn.y = i*btn.height+10;
				btn.dataSource = AnimationName.names[i];
			}
			_compBox.on(Event.CLICK,this,onClickComp);
			Laya.stage.on(Event.CLICK,this,onClickStage);
		}
		
		//点击舞台 移动到指定位置
		private function onClickStage(e:Event):void
		{
			var dx:Number = e.stageX-_roleAni.x;
			var dy:Number = e.stageY-_roleAni.y;
			var angle:Number = Math.atan2(dy,dx);
			angle = angle/Math.PI*180;
			trace("angle=",angle);
			var dist:Number = Math.sqrt((dx*dx+dy*dy));
			var perms:int = 1500;//移动100像素的距离需要的毫秒数
			var time:Number = dist/100*perms; 
			_roleAni.play(0,true,AnimationName.MOVE);
			Tween.clearTween(_roleAni);
			Tween.to(_roleAni,{x:e.stageX,y:e.stageY},time,null,Handler.create(this,onWalkEnd));
			
		}
		
		private function onWalkEnd():void
		{
			_roleAni.play(0,true,AnimationName.STAND);
		}
		
		private function onClickComp(e:Event):void
		{
			var btn:Button = e.target as Button;
			if(btn is Button)
			{
				Tween.clearTween(_roleAni);
				_roleAni.play(0,true,btn.dataSource);
			}
			e.stopPropagation();
		}
		private function onRoleLoaded():void
		{
			
		}
		
		private function aniUrls(aniName:String,len:int):Array
		{
			var urls:Array = [];
			for (var i:int = 0; i < len; i++) 
			{
				urls.push("role/"+aniName+i+".png");
			}			
			return urls;			
		}
		
	}
}

class AnimationName{
	public static const DIE:String = "die";
	public static const ATTACK:String = "attack";
	public static const MOVE:String = "move";
	public static const STAND:String = "stand";
	public static const names:Array = [
		DIE,
		ATTACK,
		MOVE,
		STAND,
	];
}